package com.uniledger.exchange.bank;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.widget.Button;

import com.uniledger.exchange.R;
import com.uniledger.exchange.bank.fragment.AddFragment;
import com.uniledger.exchange.bank.fragment.ConfirmFragment;
import com.uniledger.exchange.bank.fragment.OnCardListener;
import com.uniledger.exchange.bank.fragment.SmsCodeFragment;
import com.uniledger.exchange.base.activity.BaseActivity;
import com.uniledger.exchange.home.scheme.WithdrawActivity;

import butterknife.BindString;
import butterknife.BindView;

/**
 * 增加一张新的银行卡
 */
public class AddBankActivity extends BaseActivity implements OnCardListener {

    public static final int MANGER_CARD_CODE = 100;

    public static void start(Activity activity, int flag) {
        Intent intent = new Intent(activity, WithdrawActivity.class);
        activity.startActivityForResult(intent, MANGER_CARD_CODE);
    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, AddBankActivity.class);
        activity.startActivityForResult(intent, MANGER_CARD_CODE);
    }

    @BindView(R.id.btn_confirm_next)
    Button next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank);
    }

    @BindString(R.string.card_add_bar_title)
    String title;

    @Override
    public void initBar() {
        super.initBar();
        getSupportActionBar().setTitle(title);
    }

    private void replaceFragment(Fragment fragment, String name) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.container, fragment, name);
        transaction.addToBackStack(name);
        // Commit the transaction
        transaction.commit();
    }

    @Override
    public void getParams() {
        replaceFragment(AddFragment.newInstance("", ""), "add");
    }

    @Override
    public void add(String name, String number) {
        replaceFragment(ConfirmFragment.newInstance(name, number), "confirm");
    }

    @Override
    public void confirm(BankCard card) {
        replaceFragment(SmsCodeFragment.newInstance(card, ""), "sms");
    }

    @Override
    public void success(BankCard card) {
        //TODO 绑定银行卡成功
        Intent intent = new Intent();
        intent.putExtra("BankCard", card);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public Button getButton() {
        return next;
    }

    @Override
    public void onBackPressed() {
        FragmentManager manager = getSupportFragmentManager();
        int count = manager.getBackStackEntryCount();
        if (count == 1) {
            finish();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("add");
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}
